// packageH/recommend_reward/recommend_reward.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 2, // 绑定当前选中标签的标识符
    info: {}, //用户信息
    amount: {}, //结算金额
    listData: [], //列表数据
    status: 2, // 未结算 0   已结算1  失效-1
    json: {},

    // more
    isLoadMore: true, // 判断是否要加载更多的标识
    page: 1, //当前的页数
    total_page: 0 // 总共有多少页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdata();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page >= this.data.total_page) {
      this.setData({
        isLoadMore: false
      });
      return;
    } else if(this.data.isLoadMore){
      let pages = this.data.page + 1;
      this.setData({
        page: pages,
      });
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  change(e) {
    this.setData({
      json: {
        status: e.detail.name == 2 ? null : e.detail.name,
      },
      active: e.detail.name,
      page: 1,
      total_page: 0,
      listData: [],
      isLoadMore: true,
    });
    this.getdata();
  },

  // 获取列表
  getdata() {
    wx.showLoading({
      title: '加载中',
    });
    let urlStr = app.getNetAddresss('plugin.member-share-reward.Frontend.controllers.records.index');
    app._postNetWork({
      url: urlStr,
      data: this.data.json,
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        if (res.result == 1) {
          this.setData({
            info: res.data.info,
            amount: res.data.amount,
            listData: res.data.list.data, //数据列表
            total_page: res.data.list.last_page,
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },

  // 获取更多数据
  getMoreData() {
    wx.showLoading({
      title: '加载中',
    });
    this.setData({
      json: {
        page: this.data.page,
        status: this.data.active == 2 ? null : this.data.active,
      }
    });
    let urlStr = app.getNetAddresss('plugin.member-share-reward.Frontend.controllers.records.search');
    app._postNetWork({
      url: urlStr,
      data: this.data.json,
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        if (res.result == 1) {
          let nextarr = res.data.data;
          this.setData({
            listData: this.data.listData.concat(nextarr),
            page: res.data.current_page,
            last_page: res.data.last_page,
            isLoadMore: true,
          });
        } else {
          let pages = this.data.page - 1;
          this.setData({
            page: pages,
            isLoadMore: false,
          });
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  }
});